package com.hfs.fence

import android.content.Context
import android.graphics.Color
import android.util.Log
import com.amap.api.location.AMapLocation
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption
import com.amap.api.location.AMapLocationListener
import com.amap.api.maps.AMap
import com.amap.api.maps.model.CircleOptions
import com.amap.api.maps.model.LatLng
import com.amap.api.maps.model.MarkerOptions
import com.amap.api.maps.model.MyLocationStyle

/**
 * 定位工具类
 */
class LocationUtil : AMapLocationListener {
    private var aMapLocationClient: AMapLocationClient? = null
    private var clientOption: AMapLocationClientOption? = null
    private var callBack: ILocationCallBack? = null

    fun onDestroy() {
        if (aMapLocationClient != null) aMapLocationClient!!.onDestroy()
    }

    fun startLocate(context: Context?) {
        aMapLocationClient = AMapLocationClient(context)

        //设置监听回调
        aMapLocationClient!!.setLocationListener(this)

        //初始化定位参数
        clientOption = AMapLocationClientOption()
        //设置定位模式为高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
        clientOption!!.locationMode = AMapLocationClientOption.AMapLocationMode.Battery_Saving
        //设置是否返回地址信息（默认返回地址信息）
        clientOption!!.isNeedAddress = true
        //设置是否只定位一次,默认为false
        clientOption!!.isOnceLocation = true
        //设置是否强制刷新WIFI，默认为强制刷新
        clientOption!!.isWifiActiveScan = true
        //设置是否允许模拟位置,默认为false，不允许模拟位置
        clientOption!!.isMockEnable = false
        //设置定位间隔
//        clientOption.setInterval(1000);
        aMapLocationClient!!.setLocationOption(clientOption)
        aMapLocationClient!!.startLocation()
    }

    fun setLocationCallBack(callBack: ILocationCallBack?) {
        this.callBack = callBack
    }

    //完成定位回调
    override fun onLocationChanged(aMapLocation: AMapLocation) {
        if (aMapLocation.errorCode == 0) {
            //定位成功完成回调
            val country = aMapLocation.country
            val province = aMapLocation.province
            val city = aMapLocation.city
            val district = aMapLocation.district
            val street = aMapLocation.street
            val address = aMapLocation.address
            val lat = aMapLocation.latitude
            val lgt = aMapLocation.longitude
            callBack!!.callBack(
                country + province + city + district + street,
                lat,
                lgt,
                aMapLocation
            )
            Log.d(TAG, "定位位置: " + country + province + city + district + street + address)
            Log.d(TAG, "经纬度: $lat------$lgt")
        } else {
            //显示错误信息ErrCode是错误码，errInfo是错误信息，详见错误码表。
            Log.e(
                TAG, "location Error, ErrCode:"
                        + aMapLocation.errorCode + ", errInfo:"
                        + aMapLocation.errorInfo
            )
        }
    }

    /**
     * 自定义图标
     *
     * @return
     */
    fun getMarkerOption(str: String?, lat: Double, lgt: Double): MarkerOptions {
        val markerOptions = MarkerOptions()
        markerOptions.position(LatLng(lat, lgt))
        markerOptions.period(100)
        //        markerOptions.draggable(true);
        return markerOptions
    }

    //绘制围栏
    fun circle(aMap: AMap, v1: Double, v2: Double, m: Int) {
        aMap.removecache()
        val latLng = LatLng(v1, v2)
        aMap.addCircle(
            CircleOptions().center(latLng)
                .radius(m.toDouble()).strokeColor(Color.parseColor("#3F51B5"))
                .fillColor(Color.argb(100, 29, 161, 242)).strokeWidth(5f)
        )
    }

    //    隐藏定位显示的圆圈
    fun set(aMap: AMap) {
        val locationStyle = MyLocationStyle()
        locationStyle.strokeColor(Color.argb(0, 0, 0, 0))
        locationStyle.radiusFillColor(Color.argb(0, 0, 0, 0))
        locationStyle.strokeWidth(0f)
        aMap.myLocationStyle = locationStyle
    }

    interface ILocationCallBack {
        fun callBack(str: String?, lat: Double, lgt: Double, aMapLocation: AMapLocation?)
    }

    companion object {
        const val GEOFENCE_BROADCAST_ACTION = "com.demo.punch.broadcast"
        private const val TAG = "LocationUtil"
    }
}